<template>
	<a-card :bordered="false" :body-style="{ 'padding-bottom': '0px' }" class="mb-2">
		<a-form ref="formRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="卡号" name="code">
						<a-input v-model:value="searchFormState.code" allow-clear></a-input>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="卡属性" name="cardProp">
						<a-select
							v-model:value="searchFormState.cardProp"
							:options="cardPropOptions"
							placeholder="请选择卡属性"
							allow-clear
						></a-select>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="卡类型" name="cardType">
						<a-select
							v-model:value="searchFormState.cardType"
							:options="cardTypeOptions"
							placeholder="请选择卡类型"
							allow-clear
						></a-select>
					</a-form-item>
				</a-col>
			</a-row>
			<a-row :gutter="24">
				<a-col :span="8">
					<a-form-item label="创建时间：" >
						<div style="display: flex;align-items: center;">
							<a-form-item style="flex: 1;margin-right:12px;" name="startTimeCreated">
								<a-date-picker placeholder="请选择开始日期" class="block" format="YYYY-MM-DD" v-model:value="searchFormState.startTimeCreated" allow-clear/>
							</a-form-item>
							<a-form-item style="flex: 1" name="endTimeCreated">
								<a-date-picker placeholder="请选择结束日期" class="block" format="YYYY-MM-DD" v-model:value="searchFormState.endTimeCreated" allow-clear/>
							</a-form-item>
						</div>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" html-type="submit" @click="table.refresh()">查询</a-button>
					<a-button style="margin: 0 8px" @click="() => formRef.resetFields()">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
	</a-card>
	<a-card :bordered="false">
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="form.onOpen()">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
					<a-button danger @click="removeBatch()">
						删除
					</a-button>
					<a-button :loading="loading" @click="enableBatch()">
						启用
					</a-button>
					<a-button :loading="loading" @click="stopBatch()">
						停用
					</a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'icon'">
					<component :is="record.icon" />
				</template>
				<template v-if="column.dataIndex === 'cardType'">
					{{ $TOOL.tableData('CRM_CARD_TYPE', record.cardType) }}
				</template>
				<template v-if="column.dataIndex === 'status'">
					<a-tag v-if="record.status === 20" color="orange">
						{{ $TOOL.dictTypeData('CARD_STATUS', record.status + '') }}
					</a-tag>
					<a-tag v-if="record.status === 10" color="green">
						{{ $TOOL.dictTypeData('CARD_STATUS', record.status + '') }}
					</a-tag>
					<a-tag v-if="record.status <= 5" color="blue">
						{{ $TOOL.dictTypeData('CARD_STATUS', record.status + '') }}
					</a-tag>
					<a-tag v-if="record.status === 99" color="red">
						{{ $TOOL.dictTypeData('CARD_STATUS', record.status + '') }}
					</a-tag>
				</template>
				<template v-if="column.dataIndex === 'action'">
					<div>
						<a-button type="link" size="small"  @click="form.onOpen(record)">编辑</a-button>
						<a-divider type="vertical" />
						<a-dropdown :trigger="['click']">
							<a-button type="link" size="small" @click.prevent>
								更多
								<DownOutlined />
							</a-button>
							<template #overlay>
								<a-menu>
									<a-menu-item>
										<a-button type="link" size="small" :disabled="record.cardProp === 1" @click="handleRecharge(voucherForm, record)">充值</a-button>
									</a-menu-item>
									<a-menu-item>
										<a-button type="link" size="small" @click="adjustAccountForm.onOpen(record)">调账</a-button>
									</a-menu-item>
									<a-menu-item>
										<a-popconfirm title="确定要注销此卡吗？" @confirm="cancelCard(record)">
											<a-button :disabled="record.status === 99" type="link" size="small">注销</a-button>
										</a-popconfirm>
									</a-menu-item>
									<a-menu-item>
										<a-button size="small" type="link" :disabled="record.status !== 0" @click="openActivateModal(record)">激活</a-button>
									</a-menu-item>
									<a-menu-item>
										<a-popconfirm title="确定要删除此卡吗？" @confirm="remove(record)">
											<a-button type="link" danger size="small">删除</a-button>
										</a-popconfirm>
									</a-menu-item>
								</a-menu>
							</template>
						</a-dropdown>
					</div>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="form" @successful="table.refresh(true)" />
	<AdjustAccountForm ref="adjustAccountForm" @successful="table.refresh(true)" />
	<VoucherForm ref="voucherForm" @successful="table.refresh(true)" />
	<a-modal
		ref="modalRef"
		title="请输入激活码"
		v-model:visible="modalVisible"
		:confirm-loading="confirmLoading"
		@ok="handleActivate"
	>
		<a-input v-model:value="inputCode" allow-clear />
	</a-modal>
</template>

<script setup name="crmCard">
	import { message } from 'ant-design-vue'
	import crmApi from '@/api/crm/crmApi'
	import tool from '@/utils/tool'
	import Form from './form.vue'
	import AdjustAccountForm from "@/views/crm/card/adjustAccountForm.vue"
	import VoucherForm from '@/views/crm/voucher/form.vue'
	let searchFormState = reactive({})
	const formRef = ref()
	const table = ref(null)
	let form = ref()
	let adjustAccountForm = ref()
	let voucherForm = ref()
	const formData = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: true }
	const modalVisible = ref(false)
	const inputCode = ref(null)
	const confirmLoading = ref(false)
	const columns = [
		{
			title: '卡Id',
			dataIndex: 'id',
			width: 80
		},
		{
			title: '卡号',
			dataIndex: 'code',
			width: 160
		},
		{
			title: '卡类型',
			dataIndex: 'cardType',
			width: 120,
			align: 'center'
		},
		{
			title: '储值余额',
			dataIndex: 'amount',
			width: 80,
			align: 'center'
		},
		{
			title: '储值本金',
			dataIndex: 'amountRecharge',
			width: 80,
			align: 'center'
		},
		{
			title: '储值赠送',
			dataIndex: 'amountPresent',
			width: 80,
			align: 'center'
		},
		{
			title: '预充值',
			dataIndex: 'precharged',
			width: 80,
			align: 'center'
		},
		{
			title: '卡状态',
			dataIndex: 'status',
			width: 100,
			align: 'center'
		},
		{
			title: '开卡门店',
			dataIndex: 'storeName',
			width: 100
		},
		{
			title: '创建时间',
			dataIndex: 'createdTime',
			ellipsis: true,
			sorter: true,
			width: 160,
			align: 'center'
		},
		{
			title: '操作',
			dataIndex: 'action',
			align: 'center'
		}
	]
	let selectedRowKeys = ref([])
	let loading = ref(false)
	// 列表选择配置
	const options = {
		alert: {
			show: false,
			clear: () => {
				selectedRowKeys = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	let cardPropOptions = tool
		.dictTypeList('CARD_PROP')
		.map((item) => {
			return {
				value: item['dictValue'],
				label: item['name']
			}
		})
	let cardTypeOptions = tool
		.tableList('CRM_CARD_TYPE')
		.map((item) => {
			return {
				value: item['id'],
				label: item['name']
			}
		})
	let cardStatusOptions = tool
		.dictTypeList('CARD_STATUS')
		.map((item) => {
			return {
				value: item['dictValue'],
				label: item['name']
			}
		})
	const handleRecharge = (form, record) => {
		record.card_type = 'CARD'
		form.onOpen(record)
	}
	// 列表数据
	const loadData = (parameter) => {
		const params = Object.assign(parameter, searchFormState)
		if (params.startTimeCreated) {
			params.startTimeCreated = params.startTimeCreated.format('YYYY-MM-DD')
		}
		if (params.endTimeCreated) {
			params.endTimeCreated = params.endTimeCreated.add(1,'day').format('YYYY-MM-DD')
		}
		return crmApi.cardPage(params).then((res) => {
			return res
		})
	}
	const openActivateModal = (record) => {
		modalVisible.value = true
		formData.value = record
	}
	const cancelCard = (record) => {
		crmApi.cancelCard(record).then(res => {
			message.success('注销成功')
			table.value.refresh(true)
		})
	}
	const handleActivate = () => {
		confirmLoading.value = true
		const params = { activateCode: inputCode.value, cardId: formData.value.id }
		crmApi.activateCard(params).then(res => {
			modalVisible.value = false
			message.success('激活成功')
			table.value.refresh(true)
		}).finally(_ => {
			confirmLoading.value = false
		})
	}
	// 删除
	const remove = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		crmApi.cardDelete(params).then(() => {
			table.value.refresh(true)
		})
	}
	// 批量删除
	const removeBatch = () => {
		if (selectedRowKeys.value.length < 1) {
			message.warning('请选择一条或多条数据')
			return false
		}
		const params = selectedRowKeys.value.map((m) => {
			return {
				id: m
			}
		})
		crmApi.cardDelete(params).then(() => {
			table.value.clearRefreshSelected()
		})
	}

	// 批量启用
	const enableBatch = () => {
		if (selectedRowKeys.value.length < 1) {
			message.warning('请选择一条或多条数据')
			return false
		}
		const params = selectedRowKeys.value.map((m) => {
			return {
				id: m
			}
		})
		crmApi.cardEnable(params).then(() => {
			table.value.clearRefreshSelected()
		})
	}

	// 批量停用
	const stopBatch = () => {
		if (selectedRowKeys.value.length < 1) {
			message.warning('请选择一条或多条数据')
			return false
		}
		const params = selectedRowKeys.value.map((m) => {
			return {
				id: m
			}
		})
		loading.value = true
		crmApi.cardStop(params).then(() => {
			table.value.clearRefreshSelected()
		}).finally(_ => {
			loading.value = false
		})
	}
</script>

<style scoped>

</style>
